package exam.card.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import exam.card.service.CardService;
import exam.card.service.impl.CardServiceImpl;
import exam.utils.JDBCUtil;

/**
 * Servlet implementation class CardController
 */
public class CardController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	CardService service=new CardServiceImpl();
		Map<String,Object>mp=new HashMap<String,Object>();
		String method=request.getParameter("method");
		
		if("addCard".equals(method)){
			String cardName=request.getParameter("cardName");
			String cardNumber=request.getParameter("cardNumber");
			String cardChip=request.getParameter("cardChip");
			String cardMoney=request.getParameter("cardMoney");
			String cardHolder=request.getParameter("cardHolder");
			String bankId=request.getParameter("bankId");
			
			service.addCard(cardName, cardNumber, cardChip, cardMoney, cardHolder, bankId, mp);
			
		}else if("findAllBank".equals(method)){
			service.findAllBank(mp);
		}else if("findAllCard".equals(method)){
			String cur=request.getParameter("cur");
    		String ps=request.getParameter("ps");
			service.findAllCard(cur, ps, mp);
		}else if("updateCard".equals(method)){
			String upStr=request.getParameter("upStr");
			service.findCardById(upStr, mp);
		}else if("updateCard1".equals(method)){
			String cardId=request.getParameter("cardId");
			String cardName=request.getParameter("cardName");
			String cardNumber=request.getParameter("cardNumber");
			String cardChip=request.getParameter("cardChip");
			String cardMoney=request.getParameter("cardMoney");
			String cardHolder=request.getParameter("cardHolder");
			String bankId=request.getParameter("bankId");
			
			service.updateCard(cardId, cardName, cardNumber, cardChip, cardMoney, cardHolder, bankId, mp);
		}else if("deleteCard".equals(method)){
			String delStr=request.getParameter("delStr");
			service.deleteCard(delStr, mp);
		}
		
		JDBCUtil.printJson(response, mp);
    }
}
